package com.swift.maintenance.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.jiyi.adapter.CommonAdapter;
import com.jiyi.adapter.ViewHolder;
import com.jiyi.bean.system.UserBean;
import com.jiyi.exception.SampleProgressObserver;
import com.jiyi.retrofit.HttpMethods;
import com.swift.base.ActivityBase;
import com.swift.maintenance.R;
import com.swift.maintenance.utils.CheckUtil;
import com.swift.maintenance.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/4/20.
 */

public class RegistActivity extends ActivityBase {
    @BindView(R.id.tvBack)
    TextView tvBack;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvSearch)
    TextView tvSearch;
    @BindView(R.id.top_layout)
    RelativeLayout topLayout;
    @BindView(R.id.etAccount)
    EditText etAccount;
    @BindView(R.id.etPassword)
    EditText etPassword;
    @BindView(R.id.etPasswordConfirm)
    EditText etPasswordConfirm;
    @BindView(R.id.etUserName)
    EditText etUserName;
    @BindView(R.id.spPermission)
    Spinner spPermission;
    @BindView(R.id.btn_regist)
    Button btnRegist;

    CommonAdapter<UserBean> stateUserBeanAdapter;
    List<UserBean> dataList = new ArrayList<UserBean>();
    String selectId ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);
        ButterKnife.bind(this);
        init();
    }

    void init()
    {
        setTop();
        setViewStatus();

        setAdapter();
        getData();
    }

    void setTop()
    {
        tvBack.setText("");
        tvTitle.setText("注册");
        tvSearch.setVisibility(View.INVISIBLE);
    }

    void setViewStatus()
    {
        etAccount.setTag(R.id.tag_view_empty, "账户名不能为空");
        etPassword.setTag(R.id.tag_view_empty, "密码不能为空");
        etPasswordConfirm.setTag(R.id.tag_view_empty, "确认密码不能为空");
        etUserName.setTag(R.id.tag_view_empty, "用户昵称不能为空");
    }

    void getData()
    {
        HttpMethods.getInstance().getRegistInfo().
                subscribe(new SampleProgressObserver<List<UserBean>>(this) {
                    @Override
                    public void onNext(List<UserBean> userBeans) {
                        if (userBeans != null && userBeans.size() >0)
                        {
                            selectId = userBeans.get(0).getId();
                            dataList.clear();
                            dataList.addAll(userBeans);
                            stateUserBeanAdapter.notifyDataSetChanged();
                        }

                    }
                });
    }

    void setAdapter()
    {
        stateUserBeanAdapter = new CommonAdapter<UserBean>(this, dataList, R.layout.item_spinner) {
            @Override
            public void convert(ViewHolder holder, UserBean t) {
                holder.setText(R.id.state_name, t.getName());
            }
        };
        spPermission.setAdapter(stateUserBeanAdapter);
        spPermission.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                UserBean temp = (UserBean) parent.getAdapter().getItem(position);
                selectId = temp.getId();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    void regist()
    {

        if (CheckUtil.checkEditText(etAccount,etPassword,etPasswordConfirm,etUserName))
        {
            if (etPassword.getText().toString().length()<6)
            {
                ToastUtil.getInstances().showMsg("密码长度不能小于6位数");
                etPassword.requestFocus();
                return;
            }

            if (!etPassword.getText().toString().equals(etPasswordConfirm.getText().toString()))
            {
                ToastUtil.getInstances().showMsg("确认密码与密码不一致");
                etPasswordConfirm.requestFocus();
                return;
            }
            else
            {
                HttpMethods.getInstance().regist(etAccount.getText().toString(),etPassword.getText().toString(),selectId,etUserName.getText().toString())
                        .subscribe(new SampleProgressObserver<Object>(this) {
                            @Override
                            public void onNext(Object o) {
                                ToastUtil.getInstances().showMsg("注册成功");
//                                Intent intent = new Intent(RegistActivity.this,PerfectInformation.class);
//                                startActivity(intent);
                                finish();
                            }
                        });
            }
        }
    }



    @OnClick({R.id.btn_regist,R.id.tvBack})
    public void onClick(View view)
    {
        switch (view.getId())
        {
            case R.id.btn_regist:
                regist();
                break;
            case R.id.tvBack:
                finish();
                break;
        }
    }
}
